package interview

import (
	"github.com/gin-gonic/gin"
	crea "go-interview-forum/model/interview"
	"go-interview-forum/resp"
	"go-interview-forum/service/interview"
	"strconv"
)

type Inter struct {
	CategoriesId int64 `json:"cate_id"`
	PId          int64 `json:"p_id"`
}

// InterviewList 面试题列表
func InterviewList(c *gin.Context) {
	value, exists := c.Get("userId")
	if !exists {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	userId, err := strconv.Atoi(value.(string))
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	var info Inter
	err = c.ShouldBindJSON(&info)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}

	list, err := interview.InterviewLists(info.CategoriesId, int64(userId))
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}

	resp.SuccessResp(c, list)
}

// InterviewCreate 创建面试题
func InterviewCreate(c *gin.Context) {
	var info crea.Interview
	err := c.ShouldBind(&info)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}

	err = interview.CreateInterview(info)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}
	resp.SuccessResp(c, nil)
}
